<template>
  <div v-loading="loading" class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="buyerName">
        <el-input
          v-model="queryParams.buyerName"
          placeholder="请输入收货人"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          :disabled="info.status !== 0"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >加入订单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="info.status !== 0"
          type="primary"
          icon="el-icon-upload2"
          size="mini"
          @click="batch_deal(1)"
        >批量推送</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="info.status !== 0"
          type="primary"
          icon="el-icon-upload2"
          size="mini"
          @click="batch_deal(2)"
        >批量支付单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="info.status !== 0"
          type="danger"
          icon="el-icon-download"
          size="mini"
          @click="batch_deal(2)"
        >批量撤销</el-button>
      </el-col>
      <el-col :span="1.5" style="float: right">
        <el-button
          :disabled="info.status !== 0"
          type="success"
          icon="el-icon-circle-check"
          size="mini"
          @click="finishTask"
        >完成批次</el-button>
      </el-col>
    </el-row>

    <el-table :data="list" style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="收货人" align="center" prop="contactName" />
      <el-table-column label="推送状态" align="center" :formatter="statusFormat" />
      <el-table-column label="推送信息" align="center" prop="error_msg" />
      <el-table-column label="订单时间" align="center" width="180" prop="orderDate" />
      <el-table-column label="更新时间" align="center" prop="modifyTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="primary"
            @click="see(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="danger"
            :disabled="scope.row.logistics_status === 3||scope.row.logistics_status === 4 ? false : true"
            @click="moveOrder(scope.row.orderId)"
          >移除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 查看详情 -->
    <LogisticsDetails :show.sync="show" :form="form" />
    <!--    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />-->

    <!-- 修改进度 -->
    <el-dialog title="推送订单" :visible.sync="open" width="820px" append-to-body>

      <el-transfer
        v-model="value"
        filterable
        filter-placeholder="请输入关键字"
        :titles="titles"
        :data="data"
      >
        <span slot-scope="{ option }">{{ option.key }} - {{ option.label }}</span>
      </el-transfer>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addOrder">加 入</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
.el-transfer-panel{
  width: 300px;
  height: 450px;
}
.el-transfer-panel__list.is-filterable{
  height: 400px;
}
</style>
<script>
import { taskInfo, addOrderlist, addOrder, pushOrder, cancelOrder, moveOrder, finishTask } from '@/api/logistics'
import { orderInfo } from '@/api/order'
export default {
  name: 'Detail',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      info: {},
      // 表格数据
      list: [],
      open: false,
      data: [],
      value: [], // 选中值
      old_value: [], // 原选中值
      titles: ['待添加订单', '已添加订单'],
      // 日期范围
      dateRange: [],
      statusOptions: [
        { label: '未推送', value: 0 },
        { label: '已完成', value: 1 },
        { label: '已推送', value: 2 },
        { label: '已失败', value: 3 },
        { label: '已加入', value: 4 }
      ],
      warehouseOptions: [
        { label: '龙潭香港仓库1', value: 'ltxgck1' },
        { label: '南京保税区仓库', value: 'njbsqck' },
        { label: '龙潭宁波保税区仓库', value: 'ningboLT' },
        { label: '泰州保税仓', value: 'taizhoubsq' }
      ],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 100,
        task_no: undefined,
        orderId: undefined,
        buyerName: undefined,
        logistics_status: undefined
      },
      ids: [],
      show: false,
      form: {}

    }
  },
  created() {
    if (this.$route.query.id) {
      this.queryParams.task_no = this.$route.query.id
      this.getList()
    } else {
      this.$router.push({
        path: '/logistics/shimei/task/index'
      })
    }
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true
      taskInfo(this.queryParams).then(response => {
        if (response.code === 10000) {
          this.list = response.data.list.data
          this.info = response.data.info
          // this.total = response.count
          this.loading = false
        }
      })
    },
    /** 查询订单详情 */
    see(row) {
      this.loading = true
      orderInfo({ orderId: row.orderId, type: 1 }).then(response => {
        this.loading = false
        if (response.code === 10000) {
          this.form = response.data
          this.show = true
        }
      })
    },
    /** 状态字典翻译 */
    statusFormat(row, column) {
      let status = row.logistics_status
      for (const d of this.statusOptions) {
        if (row.logistics_status === d.value) {
          status = d.label
          break
        }
      }
      return status
    },
    /** 仓库字典翻译 */
    warehouseFormat(row, column) {
      let warehouse = row.warehouse
      for (const d of this.warehouseOptions) {
        if (row.warehouse === d.value) {
          warehouse = d.label
          break
        }
      }
      return warehouse
    },
    /** 打开 */
    handleAdd(row) {
      this.loading = true
      const that = this
      addOrderlist({ taskNo: this.queryParams.task_no }).then(response => {
        if (response.code === 10000) {
          const data = []
          const value = []
          response.data.forEach((val, index) => {
            data.push({
              label: val.contactName,
              key: val.orderId,
              disabled: val.logistics_status !== 0
            })
            if (val.task_no === that.queryParams.task_no) {
              value.push(val.orderId)
            }
          })
          that.data = data
          that.old_value = that.value = value
          that.open = true
          that.loading = false
        } else {
          this.$message.error(response.message)
        }
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      const task_no = this.queryParams.task_no
      this.queryParams = {
        page: 1,
        limit: 100,
        task_no: task_no,
        orderId: undefined,
        buyerName: undefined,
        logistics_status: undefined
      }
      this.handleQuery()
    },
    /** 提交时间改变 */
    datePick(value) {
      if (value) {
        this.queryParams.start_time = value[0]
        this.queryParams.end_time = value[1]
      } else {
        this.queryParams.start_time = undefined
        this.queryParams.end_time = undefined
      }
    },
    /** 选中的项 */
    handleSelectionChange(val) {
      const ids = []
      val.map((item, index, val) => {
        ids.push(item.orderId)
      })
      this.ids = ids
    },
    /** 关闭 */
    cancel() {
      this.open = false
      this.addInfo = {}
    },
    /** 单个推送 */
    pushOne(orderId) {
      const orders = [orderId]
      this.addOrder(orders)
    },
    /** 批量操作 */
    batch_deal(type) {
      const that = this
      if (that.ids.length === 0) {
        that.$message.error('请选择订单')
        return
      }
      // console.log(data)
      that.$confirm('是否操作' + that.ids[0] + '...等订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        // 批量推送
        if (type === 1) {
          that.pushOrder(that.ids)

        // 批量撤销
        } else if (type === 2) {
          that.cancelOrder(that.ids)
        }
      }).catch(() => {})
    },
    /** 推送订单 */
    pushOrder(list) {
      const that = this
      that.loading = true
      pushOrder({ orders: list, task_no: that.queryParams.task_no }).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.getList()
        }
        that.loading = false
      })
    },
    /** 撤销订单 */
    cancelOrder(list) {
      const that = this
      that.loading = true
      cancelOrder({ orders: list, task_no: that.queryParams.task_no }).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.getList()
        }
        that.loading = false
      })
    },
    /** 移除订单 */
    moveOrder(orderId) {
      const that = this
      that.loading = true
      moveOrder({ orders: [orderId], task_no: that.queryParams.task_no }).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.getList()
        }
        that.loading = false
      })
    },
    addOrder() {
      const list = this.value.filter(items => {
        if (!this.old_value.includes(items)) return items
      })
      if (list.length === 0) {
        this.$message.error('请选择需要推送的订单')
        return false
      }
      const that = this
      that.loading = true
      addOrder({ orders: list, task_no: that.queryParams.task_no }).then(response => {
        that.data = []
        that.old_value = that.value = []
        that.open = false
        that.loading = false
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.getList()
        }
      })
    },
    /** 完成批次 */
    finishTask() {
      const that = this
      // console.log(data)
      that.$confirm('是否完成批次' + that.queryParams.task_no + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        finishTask({ task_nos: [that.queryParams.task_no] }).then(response => {
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          }
          that.loading = false
        })
      }).catch(() => {})
    }
  }
}
</script>
